import Vue from 'vue'
import VueRouter from 'vue-router'

import { $SysMenuApi } from '@/network/menu/SysMenuApi';

let MENU_MAP = {};

// (async function() {
//   const res = await $SysMenuApi.getMenuInfoMap();
//   MENU_MAP = {...res.data};
// }())

// console.log(MENU_MAP);


const baseRoutes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/Home'),
    children: [
      {
        path: '/systemSetting',
        name: 'systemSetting',
        component: () => import('@/views/system/SystemSetting'),
        children: [
          {
            path: '/systemSetting/menuList',
            name: 'MenuList',
            component: () => import('@/views/system/menus/MenuList')
          },
          {
            path: '/systemSetting/basicAuth',
            name: 'BasicAuth',
            component: () => import('@/views/authority/BasicAuth')
          },
          {
            path: '/systemSetting/dataAuth',
            name: 'DataAuth',
            component: () => import('@/views/authority/DataAuth'),

          },
          {
            path: '/systemSetting/dsConns',
            name: 'ConnectionManage',
            component: () => import('@/views/system/datasource/ConnectionManage')
          },
          {
            path: '/systemSetting/database',
            name: 'DatabaseManage',
            component: () => import('@/views/system/datasource/DatabaseManage')
          }
        ]
      },
      {
        path: '/featureCenter',
        name: 'featureCenter',
        component: () => import('@/views/feature/FeatureCenter'),
        children: [
          {
            path: '/featureCenter/basicType',
            name: 'basicType',
            component: () => import('@/views/feature/BasicTypeManage'),
            meta: {
              name: 'basic',
              description: '基础类型'
            }
          },
          {
            path: '/featureCenter/businessObj',
            name: 'businessType',
            component: () => import('@/views/feature/BusinessFeatureManage'),
            meta: {
              name: 'business',
              description: '业务对象'
            }
          },
          {
            path: '/featureCenter/busiAttrManage',
            name: 'BusiAttrManage',
            component: () => import('@/views/feature/BusiAttrManage'),
            meta: {
              name: 'busiAttr',
              description: '业务属性'
            }
          },
          {
            path: '/featureCenter/featurePublishManage',
            name: 'FeaturePublishManage',
            component: () => import('@/views/feature/FeaturePublishManage'),
            meta: {
              name: 'featurePublishManage',
              description: '业务对象发布管理'
            }
          },
        ]
      },
      {
        path: '/knowledgeCenter',
        name: 'knowledgeCenter',
        component: () => import('@/views/knowledge/KnowledgeCenter'),
        children: [
          {
            path: '/knowledgeCenter/library',
            name: 'Library',
            component: () => import('@/views/knowledge/Library'),
            meta: {
              name: 'Library',
              description: '知识库'
            }
          }
        ]
      },
    ]
  },
]

async function getRoutesWithMeta() {

  try {
    const res = await $SysMenuApi.getMenuInfoMap();
    const menuData = res.data;

    function setRouteMeta(routes) {
      return routes.map(route => {
        const newRoute = {
          ...route,
          meta: {
            id: menuData[route.path]?.id || "",
            name: menuData[route.path]?.name || "",
            description: menuData[route.path]?.description || "",
            text: menuData[route.path]?.text || "",
          }
        }
        if (newRoute.children && newRoute.children.length > 0) {
          newRoute.children = setRouteMeta(newRoute.children)
        }

        return newRoute;
      })
    }

    return setRouteMeta(baseRoutes);

  } catch (error) {
     console.error('获取菜单数据失败或设置路由meta出错:', error);
    return baseRoutes;
  }
  
}



let originPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(this, location, () => { }, () => { });
  }
}

const router = new VueRouter({
  routes: await getRoutesWithMeta()
})

Vue.use(VueRouter);

export default router
